package com.msb.mall.search.controller;

import com.msb.mall.common.dto.es.SkuESModel;
import com.msb.mall.common.exception.BizCodeEnume;
import com.msb.mall.common.utils.R;
import com.msb.mall.search.service.ElastaticSearchSaveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Description 存储商城数据到Elasticsearch的服务
 * @Author liujianlin <157429758@qq.com>
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2024/7/14
 */
@RequestMapping("/search/save")
@RestController
public class ElastaticSearchSaveController {
    private static final Logger log = LoggerFactory.getLogger(ElastaticSearchSaveController.class);

    @Autowired
    private ElastaticSearchSaveService elastaticSearchSaveService;

    /**
     * 存储商品上架信息到Elasticsearch服务的接口
     * @param skuESModels
     * @return
     * @throws IOException
     */
    @RequestMapping("/product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModels) {
        Boolean b = false;
        try {
            b = elastaticSearchSaveService.productStatusUp(skuESModels);
        } catch (IOException e) {
            log.error("Elasticsearch存储商品上架，更新索引库失败：{}", e);
            return R.error(BizCodeEnume.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        // 商品上架，更新索引库
        return R.ok();
    }
}
